from flask import g, Blueprint, redirect, url_for, request, jsonify, session, render_template
from flask_login import login_user, logout_user

from exts import db
from models import Capture, PassersBy

bp = Blueprint("api_passers_by", __name__, url_prefix="/api/passers_by")


@bp.route("/set_data", methods=['POST'])
def set_data():
    person_id = request.form.get('id')
    up_or_down = request.form.get('up_or_down')
    count = request.form.get('count')

    if int(count) == 1:
        pb = PassersBy(
            up_or_down=up_or_down,
            person_id=person_id,
            count=count,
            person_label=db.session.query(Capture).filter(Capture.person_id == person_id).order_by(Capture.id.desc()).first().person_label
        )
        db.session.add(pb)
    else:
        for i in person_id:
            pb = PassersBy(
                up_or_down=up_or_down,
                person_id=i,
                count=1,
                person_label=db.session.query(Capture).filter(Capture.person_id == i).order_by(Capture.id.desc()).first().person_label
            )
            db.session.add(pb)

    db.session.commit()

    return "success"
